package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.util.List;
import model.objects.GameObject;
import model.objects.Level;
import control.GameController;

/**
 * Panel containing the main portion of the game.
 * 
 * @author Sam Rang
 *
 */
@SuppressWarnings("serial")
public class Canvas extends ViewPanel {
    private Image myBackground;
    private Level myLevel;
    private GameController myController;
    /**
     * Initializes the controller and background but not the Level
     * 
     * @param controller Controller parent
     * @param backgroundFile String representing the background of the Level
     */
    public Canvas (GameController controller, String backgroundFile) {
        myBackground = getBackground(backgroundFile);
        myController = controller;
    }

    /**
     * Sets the level in the Canvas
     * 
     * @param level current level
     */
    public void setLevel (Level level) {
        myLevel = level;
    }

    @Override
    public void paint (Graphics g) {        
        if (myLevel == null) {
            return;
        }
        
        List<GameObject> list = myLevel.getObjects();
        int dx = (int)myLevel.getHeroLocation().getX();
        int dy = (int)myLevel.getHeroLocation().getY();
        Dimension dim = myController.getWindowSize();
        g.drawImage(myBackground, 0, 0,
                (int)dim.getWidth(), (int)dim.getHeight(),
                0, 0, myBackground.getWidth(null), myBackground.getHeight(null), null);
        for (GameObject obj : list) {
            Rectangle2D.Double bounds = (Rectangle2D.Double) obj.getBounds();
            Image img = obj.getImage();
            g.drawImage(img, (int)bounds.getX() - dx, (int)bounds.getY(),
                    (int)bounds.getMaxX() - dx, (int)bounds.getMaxY(),
                    0, 0, (int)bounds.getWidth(), (int)bounds.getHeight(), null);
        }
    }
}
